package vn.orderonline.web.admin.controller;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import vn.orderonline.com.core.business.menufood.IMenuFood;
import vn.orderonline.com.core.mvc.model.MenuFoodDto;

@Controller
@RequestMapping("/menufood")
public class MenuFoodController {

	private static final Logger LOGGER = Logger
			.getLogger(MenuFoodController.class);

	@Autowired
	private IMenuFood iMenuFood;

	@RequestMapping(value = "/form", method = RequestMethod.GET)
	public String createForm(@RequestParam Integer restaurantId,
			@RequestParam String restaurantName, ModelMap model) {
		LOGGER.info("START GOING TO MENU FOOD FORM!");
		MenuFoodDto menuFood = null;
		try {
			model.addAttribute("list",
					iMenuFood.getListMenuFoodByRestaurantId(restaurantId));
			menuFood = new MenuFoodDto();
			menuFood.setRestaurantId(restaurantId);
			menuFood.setRestaurantName(restaurantName);
			model.addAttribute("menuFood", menuFood);
		} catch (Exception e) {
			LOGGER.error("ERROR WHEN GOING TO MENU FOOD FORM!", e);
		}
		LOGGER.info("END GOING TO MENU FOOD FROM!");
		return "/menufood/menufood";
	}

	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public String addMenuFood(@ModelAttribute MenuFoodDto menuFood,
			RedirectAttributes redirectAttris) {
		LOGGER.info("STARTING ADD MENU FOOD!");
		try {
			iMenuFood.saveMenuFood(menuFood);
			redirectAttris.addAttribute("restaurantId", menuFood.getRestaurantId());
			redirectAttris.addAttribute("restaurantName", menuFood.getRestaurantName());
		} catch (Exception e) {
			LOGGER.error("ERROR WHEN ADD MENU FOOD!", e);
		}
		LOGGER.info("END ADD MENU FOOD!");
		return "redirect:/menufood/form";
	}

	@RequestMapping(value = "/isexisted", method = RequestMethod.GET)
	@ResponseBody
	public String checkExisted(@RequestParam Integer restaurantId,
			@RequestParam String menuFoodName) {
		LOGGER.info("START CHECK EXISTED MENU FOOD NAME!");
		Boolean flag = null;
		try {
			flag = iMenuFood.checkExistedMenuName(restaurantId, menuFoodName);
		} catch (Exception e) {
			LOGGER.error("ERROR WHEN CHECK EXISTED MENU FOOD NAME!", e);
		}
		LOGGER.info("END CHECK EXISTED MENU FOOD NAME!" + flag.toString());
		return flag.toString();
	}

	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	@ResponseBody
	public String deleteMenuFood(@RequestParam Integer restaurantId,
			@RequestParam Integer menuFoodId) {
		LOGGER.info("START DELETE MENU FOOD!");
		try {
			iMenuFood.deleteMenuFood(restaurantId, menuFoodId);
		} catch (Exception e) {
			LOGGER.error("ERROR WHEN DELETE MENU FOOD!", e);
		}
		LOGGER.info("END DELETE MENU FOOD!");
		return "true";
	}

	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public String updateMenuFood(@ModelAttribute MenuFoodDto menuFood,
			RedirectAttributes redirectAttris) {
		LOGGER.info("START UPDATE MENU FOOD!");
		try {
			iMenuFood.updateMenuFood(menuFood.getMenuFoodId(), menuFood.getMenuFoodName());
			redirectAttris.addAttribute("restaurantId", menuFood.getRestaurantId());
			redirectAttris.addAttribute("restaurantName", menuFood.getRestaurantName());
		} catch (Exception e) {
			LOGGER.error("ERROR WHEN UPDATE MENU FOOD!", e);
		}
		LOGGER.info("END UPDATE MENU FOOD!");
		return "redirect:/menufood/form";
	}

	@RequestMapping(value = "/formupdate", method = RequestMethod.GET)
	public String updateForm(@RequestParam Integer restaurantId,
			@RequestParam String restaurantName,
			@RequestParam Integer menuFoodId, @RequestParam String menuFoodName, ModelMap model) {
		LOGGER.info("START CREATE FORM UPDATE MENU FOOD!");
		MenuFoodDto menuFood = null;
		try {
			menuFood = new MenuFoodDto();
			menuFood.setRestaurantId(restaurantId);
			menuFood.setRestaurantName(restaurantName);
			menuFood.setMenuFoodId(menuFoodId);
			menuFood.setMenuFoodName(menuFoodName);
			model.addAttribute("menuFood", menuFood);
		} catch (Exception e) {
			LOGGER.error("ERROR WHEN CREATE FORM UPDATE MENU FOOD!", e);
		}
		LOGGER.info("END CREATE FORM UPDATE MENU FOOD!");
		return "/menufood/updatemenufood";
	}
}
